home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / WTEK0593.ZIP;1 / CALLBACK.ZIP / OBJCALLB.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-10  |  4.1 KB  |  150 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //  OBJCALLB.CPP
  4. //
  5. //  Object Thunk functions
  6. //
  7. //--------------------------------------------------------------------------
  8.  
  9. #include "objcallb.h"
  10. #include <mem.h>
  11.  
  12.  
  13. const int CNTJumpInstance=64;
  14.  
  15. //
  16. // AllocCSToDSAlias was not declared in windows.h
  17. //
  18. extern "C" WORD FAR PASCAL AllocCSToDSAlias(WORD);
  19.  
  20. //--------------------------------------------------------------------------
  21. //
  22. //  Define data structures.
  23. //
  24. //--------------------------------------------------------------------------
  25.  
  26. //
  27. //  This is the Jump Block, which is basically a thunk with the
  28. //  following assembler code:
  29. //
  30. //      mov     bx, SEGT
  31. //      mov     es, bx
  32. //      mov     bx, OFFT
  33. //      jmp     FAR FarJump
  34. //
  35.  
  36.  
  37. #pragma option -a-          // force byte alignment
  38.  
  39. typedef struct JumpThunk {
  40.     BYTE    ins1;       // 0xBB
  41.     WORD    wThisSeg;
  42.     BYTE    ins2[3];    // 0x8E 0xC3 0xBB
  43.     WORD    wThisOff;
  44.     BYTE    ins3;       // 0xEA
  45.     union   Ptr {
  46.         struct JumpThunk FAR *Next;
  47.         FARPROC fpJump;
  48.     } ptr;
  49.     BYTE    reserved[3];// make an even 16 bytes
  50. } FAR *PJumpThunk;
  51.  
  52. #pragma option -a.          // default alignment
  53.  
  54.  
  55. static char HeaderSignature[] = "ThunkMaster";
  56.  
  57. typedef struct JumpBlock {
  58.     WORD    NextBlock;
  59.     BYTE    HeaderInfo[sizeof(HeaderSignature)];
  60.     JumpThunk   Instances[CNTJumpInstance];
  61. } FAR *PJumpBlock;
  62.  
  63. //--------------------------------------------------------------------------
  64. //
  65. //  Define global variables
  66. //
  67. //--------------------------------------------------------------------------
  68.  
  69. WORD InstBlockList = 0;
  70. PJumpThunk InstFreeList = NULL;
  71.  
  72.  
  73. //--------------------------------------------------------------------------
  74. //
  75. //  MakeObjectThunk - This allocates an instance thunk
  76. //
  77. //--------------------------------------------------------------------------
  78. FARPROC WINAPI MakeObjectThunk(FARPROC CallBack, void far *fpObject)
  79. {
  80.     PJumpBlock Block;
  81.     PJumpThunk Instance;
  82.     FARPROC ObjInstance;
  83.  
  84.     //
  85.     //  If there are no free object thunk blocks available,
  86.     //  allocate a new one.
  87.     //
  88.     if (InstFreeList==NULL) {
  89.         Block = (PJumpBlock) GlobalLock(GlobalAlloc(
  90.                  GMEM_FIXED | GMEM_DDESHARE, sizeof(JumpBlock)));
  91.         Block->NextBlock = InstBlockList;
  92.         _fmemcpy(Block->HeaderInfo, HeaderSignature, 
  93.             sizeof(HeaderSignature));
  94.  
  95.         Instance = Block->Instances;
  96.  
  97.         for(int i=0; i<CNTJumpInstance; ++i, ++Instance) {
  98.             Instance->ins1    = 0xBB;
  99.             Instance->ins2[0] = 0x8E;
  100.             Instance->ins2[1] = 0xC3;
  101.             Instance->ins2[2] = 0xBB;
  102.             Instance->ins3    = 0xEA;
  103.             Instance->ptr.Next = InstFreeList;
  104.             InstFreeList = Instance;
  105.         }
  106.  
  107.         InstBlockList = SELECTOROF(Block);
  108.  
  109.         PrestoChangoSelector(SELECTOROF(Block), SELECTOROF(Block));
  110.     }
  111.  
  112.     ObjInstance = (FARPROC)InstFreeList;
  113.  
  114.     Instance = (PJumpThunk) MAKELP(
  115.                     AllocCSToDSAlias(SELECTOROF(InstFreeList)),
  116.                     OFFSETOF(InstFreeList));
  117.  
  118.     InstFreeList = Instance->ptr.Next;
  119.     Instance->ptr.fpJump = CallBack;
  120.     Instance->wThisSeg   = SELECTOROF(fpObject);
  121.     Instance->wThisOff   = OFFSETOF(fpObject);
  122.     FreeSelector(SELECTOROF(Instance));
  123.  
  124.     return (ObjInstance);
  125. }
  126.  
  127.  
  128. //--------------------------------------------------------------------------
  129. //
  130. //  FreeObjectThunk
  131. //
  132. //  Warning! This does NOT de-allocate Thunk blocks if they
  133. //  are fully un-used.  It probably should, but when the module
  134. //  is unloaded, they seem to be deleted anyway.
  135. //
  136. //--------------------------------------------------------------------------
  137. void WINAPI FreeObjectThunk(FARPROC P)
  138. {
  139.     PJumpThunk Instance;
  140.  
  141.     Instance = (PJumpThunk) MAKELP(
  142.             AllocCSToDSAlias(SELECTOROF(P)),
  143.             OFFSETOF(P) );
  144.  
  145.     Instance->ptr.Next = InstFreeList;
  146.     FreeSelector(SELECTOROF(Instance));
  147.     InstFreeList = (PJumpThunk)P;
  148. }
  149.  
  150.